package com.heima.ai.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.heima.ai.entity.vo.MessageVO;
import com.heima.ai.repository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ai/history")
@RequiredArgsConstructor
public class ChatHistoryController {

    private final ChatHistoryRepository chatHistoryRepository;
    private final ChatMemory chatMemory;

    /**
     * 查询会话记录列表
     * @param type 业务类型
     * @return 会话记录列表
     */
    @GetMapping("/{type}")
    public List<String> getchatIds(@PathVariable("type") String type){
        return chatHistoryRepository.getchatIds(type);
    }

    @GetMapping("/{type}/{chatId}")
    public List<MessageVO> getChatHistory(@PathVariable("type") String type,@PathVariable("chatId") String chatId){
        List<Message> messages = chatMemory.get(chatId);
        return CollectionUtil.isNotEmpty(messages)?
                messages.stream().map(MessageVO::new).toList():CollectionUtil.newArrayList();
    }
}
